<?php
/**
	Sponsor Manager (class.sponsors.php file)
    Copyright (C) <Mon Mar 03 16:16:52 CET 2008>  Marcos Ramírez <marcosramirez@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    
    Purpose:
    Last Modified:
**/

class Sponsors {
	function __construct() {
	
	}
	
	function getSponsorsList () {
		global $db;
		$result=$db->Query('SELECT * FROM My_Sponsors');
		return $Res=$db->FetchAll($result);
	}
	
	public function getSponsorListOptions () {
		global $db;
		$result=$db->query('SELECT * FROM My_Sponsors');
		while ($Res=$db->fetch())
			$options.='<option value="'.$Res['sponsorid'].'">'.$Res['name'].' ('.$Res['affiliateid'].')</option>';
		return $options;
	}
	
	function addSponsor ($url, $name, $affiliateid) {
		global $db;
		global $html;
		// Check if exists.
		$result=$db->query("SELECT url FROM My_Sponsors WHERE url='$url' AND name='$name' AND affiliateid='$affiliateid'");
		if ($db->error()!='') {
			$html->addError(_('Error while checking if sponsor allready exists, error detected: ').$db->error());
			return true;
		}
		
		if (is_array($Res=$db->fetch())) {
			$html->addError(_('Error, sponsor allready exists')); 
			return true;
		}
					
		
		// Insert new sponsor
		$result=$db->Query("INSERT INTO My_Sponsors 
						    (url, name, affiliateid) 
						    VALUES
						    ('$url', '$name', '$affiliateid')");
		if ($db->error()!='') {
			$html->addError(_('Error detected while inserting sponsor: ').$db->Error());
			return true;
		} else {
			$html->addInfo(_('Sponsor correctly inserted.'));
			return false;
		}
		
	}
	
	public function totalSponsors () {
		global $sm;
		$result=$sm->Query("SELECT COUNT(url) FROM My_Sponsors");
		$Res=$sm->Fetch($result);
		return $Res['COUNT(url)'];
	}
}
?>